package com.example.demo.service.impl;

import com.example.demo.model.Dishes;
import com.example.demo.model.User;
import com.example.demo.repository.AuthRepository;
import com.example.demo.repository.DishesRepository;
import com.example.demo.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

@Service
public class AuthServiceImpl implements AuthService {
    @Autowired
    private final AuthRepository userRepository;
    private final DishesRepository dishesRepository;

    @Autowired
    public AuthServiceImpl(AuthRepository userRepository, DishesRepository dishesRepository) {
        this.userRepository = userRepository;
        this.dishesRepository = dishesRepository;
    }


    @Override
    public void register(User user) {
        user.setPassword(encryptPassword(user.getPassword()));
        userRepository.save(user);
    }

    @Override
    public String login(String name, String password) {
        User user = userRepository.findByName(name);
        if (user == null || !encryptPassword(password).equals(user.getPassword())) {
            throw new IllegalArgumentException("Invalid name or password");
        }
        // 在这里生成并返回用户的认证 token
        return generateToken(user.getUserid());
    }

    @Override
    public void addDishToUser(Long userId, Long dishId) {
        User user = userRepository.findByUserId(userId);
        Dishes dish = dishesRepository.findByDishId(dishId);

        if (user != null && dish != null) {
            user.getDishes().add(dish);
            userRepository.save(user);
        }
    }

    @Override
    public String encryptPassword(String password) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            byte[] encodedHash = digest.digest(password.getBytes());
            return Base64.getEncoder().encodeToString(encodedHash);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("Error while hashing password", e);
        }
    }

    @Override
    public String generateToken(Long userId) {
        // 在这里生成用户的认证 token
        // 可以使用 JWT 或其他认证方案来生成 token
        return "";
    }

    @Override
    public void shareDishWithUser(Long userId, Long dishId, Long otherUserId) {
        User user = userRepository.findByUserId(userId);
        Dishes dish = dishesRepository.findByDishId(dishId);
        User otherUser = userRepository.findByUserId(otherUserId);

        if (user != null && dish != null && otherUser != null) {
            user.shareDishWithUser(dish, otherUser);
            userRepository.save(user);
            userRepository.save(otherUser);
        }
    }

    @Override
    public void addViewDishToUser(Long userId, Long dishId) {
        User user = userRepository.findByUserId(userId);
        Dishes dish = dishesRepository.findByDishId(dishId);

        if (user != null && dish != null) {
            user.getViewedDishes().add(dish);
            dish.increaseViews();
            userRepository.save(user);
            dishesRepository.save(dish);
        }
    }
}
